package com.bootdo.system.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bootdo.system.domain.CompanyPictureDO;
import com.bootdo.system.domain.WechatPicDO;
import com.bootdo.system.service.WechatPicService;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 朋友圈图片
 * @author lzy
 */
 
@Controller
@RequestMapping("/system/pic")
public class WechatPicController {
	@Autowired
	private WechatPicService wechatpicService;
	
	@GetMapping()
	@RequiresPermissions("system:pic:pic")
	String Pic(){
	    return "system/pic/pic";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:pic:pic")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<WechatPicDO> picList = wechatpicService.list(query);
		int total = wechatpicService.count(query);
		PageUtils pageUtils = new PageUtils(picList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:pic:add")
	String add(){
	    return "system/pic/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:pic:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		WechatPicDO pic = wechatpicService.get(id);
		model.addAttribute("pic", pic);
	    return "system/pic/edit";
	}
	
	@GetMapping("/uploadpre")
	@ResponseBody
	List<WechatPicDO> uploadpre(@RequestParam Integer id, Model model) {
		List<WechatPicDO> list = wechatpicService.getByUserId(id);
		return list;
	}
	
	
	/**
	 * 编辑小程序模板轮播图片
	 */
	@ResponseBody
	@PostMapping("/updatePic")
	public R savePic(@RequestParam("img") MultipartFile[] img,Integer id){
		 try {
				for (MultipartFile file : img) {
					String url =null;
					try {
						url = OssUtil.fileUpload(file);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					WechatPicDO wechatPicDO=new WechatPicDO();
					wechatPicDO.setUrl(url);
					wechatPicDO.setUserId(id);
					wechatPicDO.setAddTime(new Date());
					wechatpicService.save(wechatPicDO);
				}
				return R.ok();
		} catch (Exception e) {
			return R.error();
		}
		
			
			
	}
	
	
	
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:pic:add")
	public R save( WechatPicDO pic){
		if(wechatpicService.save(pic)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:pic:edit")
	public R update(WechatPicDO pic){
		wechatpicService.update(pic);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:pic:remove")
	public R remove( Integer id){
		if(wechatpicService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/removeByid")
	@ResponseBody
	public R removeByid( Integer id){
		if (wechatpicService.removeByid(id)>0) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", id);
			List<WechatPicDO> list = wechatpicService.list(map);
			for (WechatPicDO companyPictureDO : list) {
				String imgUrl1=companyPictureDO.getUrl().substring(45, 62);
				OssUtil.deleteBucketInfo(imgUrl1);
			}
		  }
		return R.error();
	}
	
	
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:pic:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){
		wechatpicService.batchRemove(ids);
		return R.ok();
	}
	
	
	

}
